﻿using LiveChartsCore;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Text;
using Zhaoxi.Mobile.Models;
using System.Threading.Tasks;
using System.Net.Sockets;

namespace Zhaoxi.Mobile.ViewModels
{
    public class DeviceViewModel
    {
        public List<string> DeviceList { get; set; } =
            new List<string>();


        public ISeries[] Series { get; set; } =
        {
            new LineSeries<double>
            {
                Values = new double[] { 21, 1, 30, 5, 35, 4, 96 },
                Fill = null,
                GeometrySize=0,
                Stroke=new SolidColorPaint(new SKColor(255, 125, 0)) { StrokeThickness = 2 }
            },
            new LineSeries<double>
            {
                Values = new double[] { 12, 21, 16, 54, 25, 48, 76 },
                Fill = null,
                GeometrySize=0,
                Stroke=new SolidColorPaint(new SKColor(0, 108, 190)) { StrokeThickness = 2 }
            }
        };

        public List<VariableModel> VarList { get; set; } =
            new List<VariableModel>()
            {
                new VariableModel(),
                new VariableModel(),
                new VariableModel()
            };


        public Axis[] YAxes { get; set; } =
        {
            new Axis
            {
                MinLimit=0,MaxLimit=100,IsVisible=false
            }
        };


        public DeviceViewModel()
        {
            for (int i = 0; i < 10; i++)
            {
                DeviceList.Add("设备-" + (i + 1).ToString("00"));
            }

            Task.Run(async () =>
            {
                // 为什么 不用NModbus库  ？   包含2   通信方   串口  网口    SerialPort    Socket
                // system.io.port   在移动端用不了    ModbusRTU   ModbusTCP

                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect("192.168.1.2", 502);

                while (true)
                {
                    await Task.Delay(1000);

                    try
                    {
                        // ModbusTCP的读取请求报文
                        byte[] bytes = new byte[] {
                            0x00,0x00,
                            0x00,0x00,
                            0x00,0x06,
                            0x01,
                            0x03,
                            0x00,0x00,
                            0x00,0x03
                        };
                        socket.Send(bytes);

                        byte[] respByteHeader = new byte[6];
                        socket.Receive(respByteHeader, 0, 6, SocketFlags.None);

                        ushort len = BitConverter.ToUInt16(new byte[] { respByteHeader[5], respByteHeader[4] }, 0);
                        byte[] respbytes = new byte[len];
                        socket.Receive(respbytes, 0, len, SocketFlags.None);

                        VarList[0].Value =
                        BitConverter.ToUInt16(new byte[] { respbytes[4], respbytes[3] }, 0);
                        VarList[1].Value =
                        BitConverter.ToUInt16(new byte[] { respbytes[6], respbytes[5] }, 0);
                        VarList[2].Value =
                        BitConverter.ToUInt16(new byte[] { respbytes[8], respbytes[7] }, 0);
                    }
                    catch (Exception ex)
                    {

                    }

                }
            });
        }
    }
}
